<?php

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){
$db = new Mypdo(); ?>
<?php


$propriManager = new ProprietaireManager($db);

if(empty($_POST)){

    $listePropri = $propriManager->getList();
    ?>

    <h1>Crée une habitation</h1>

    <form method="post" action="#">

        <p>
        <h3>Information</h3>
        <label for="id_propri">Propriétaire :</label><select name="id_propri" id="id_propri" required="required">
            <option></option>
            <?php foreach($listePropri as $key=>$value){
                ?><option <?php if(!empty($_GET['idp'])){echo $_GET['idp'] == $value->getIdPropri()?'selected':'';} ?>  value="<?php echo $value->getIdPropri(); ?>"><?php echo $value->getNomPropri().' '.$value->getPrenomPropri(); ?></option><?php echo "\n";
            } ?>


        </select>
        <label for="nom_immeu">Nom de l'habitation:</label><input name="nom_immeu" type="text" id="nom_immeu" required="required" /><br />
        <label for="digicode_immeu">Digicode :</label><input type="text" name="digicode_immeu" id="digicode_immeu" required="required" /><br />
        <label for="descp_immeu">Description :</label><textarea rows="4" cols="30" name="descp_immeu" id="descp_immeu" maxlength="255"  ></textarea>

        </p>
        <p>
            <?php include_once('Form_adresse.inc.php'); ?>
        </p>

        <p><input type="reset" value="Annuler" /><input type="submit" value="Valider" /></p>
    </form>

<?php }else{
    $immeu = new Immeuble($_POST);
    $immeu->setAdresseImmeu(new Adresse($_POST));
    var_dump($immeu);

    $immeuManager = new ImmeubleManager($db);


    $id = $immeuManager->add($immeu);

    header('Location: index.php?page=112&id='.$id);

}
}else{
    header('Location: index.php');
}